<template>
  <div class="news-detail-container">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/" class="breadcrumb">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/news' }">新闻中心</el-breadcrumb-item>
      <el-breadcrumb-item>新闻详情</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 新闻主体 -->
    <div class="news-main">
      <!-- 新闻标题区 -->
      <div class="news-header">
        <h1 class="news-title">{{ newsData.title }}</h1>
        <div class="news-meta">
          <span class="news-time">
            <el-icon><Clock /></el-icon>
            {{ formatDate(newsData.publishTime) }}
          </span>
          <span class="news-author">
            <el-icon><User /></el-icon>
            {{ newsData.author || '本站编辑' }}
          </span>
          <span class="news-views">
            <el-icon><View /></el-icon>
            阅读量: {{ newsData.views || 0 }}
          </span>
        </div>
      </div>

      <!-- 新闻封面图 -->
      <div class="news-cover" v-if="newsData.coverImage">
        <el-image
            :src="newsData.coverImage"
            :preview-src-list="[newsData.coverImage]"
            fit="cover"
        />
      </div>

      <!-- 新闻正文 -->
      <div class="news-content" v-html="newsData.content"></div>

      <!-- 图片画廊 -->
      <div class="news-gallery" v-if="newsData.images && newsData.images.length">
        <h3 class="gallery-title">相关图片</h3>
        <el-row :gutter="20">
          <el-col
              v-for="(img, index) in newsData.images"
              :key="index"
              :xs="24" :sm="12" :md="8"
          >
            <el-image
                :src="img.url"
                :alt="img.caption || '新闻配图'"
                :preview-src-list="newsData.images.map(i => i.url)"
                class="gallery-image"
            >
              <template #error>
                <div class="image-error">
                  <el-icon><Picture /></el-icon>
                  <span>图片加载失败</span>
                </div>
              </template>
            </el-image>
            <p class="image-caption" v-if="img.caption">{{ img.caption }}</p>
          </el-col>
        </el-row>
      </div>

      <!-- 标签和分享 -->
      <div class="news-footer">
        <div class="news-tags">
          <el-tag
              v-for="(tag, index) in newsData.tags"
              :key="index"
              size="small"
              :type="tagTypes[index % tagTypes.length]"
          >
            {{ tag }}
          </el-tag>
        </div>
        <div class="news-share">
          <span>分享至：</span>
          <el-button type="primary" plain circle size="small">
            <el-icon><Share /></el-icon>
          </el-button>
          <el-button type="success" plain circle size="small">
            <el-icon><ChatDotRound /></el-icon>
          </el-button>
          <el-button type="warning" plain circle size="small">
            <el-icon><Star /></el-icon>
          </el-button>
        </div>
      </div>
    </div>

    <!-- 相关新闻推荐 -->
    <div class="related-news" v-if="relatedNews.length">
      <h3 class="related-title">相关推荐</h3>
      <el-row :gutter="20">
        <el-col
            v-for="(item, index) in relatedNews"
            :key="index"
            :xs="24" :sm="12" :md="8"
        >
          <el-card shadow="hover" class="related-card" @click="goToNews(item.id)">
            <el-image
                :src="item.cover"
                class="related-cover"
                fit="cover"
            />
            <div class="related-info">
              <h4>{{ item.title }}</h4>
              <p class="related-time">{{ formatDate(item.time) }}</p>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import {
  Clock,
  User,
  View,
  Picture,
  Share,
  ChatDotRound,
  Star
} from '@element-plus/icons-vue'

const router = useRouter()
const tagTypes = ['', 'success', 'info', 'warning', 'danger']

// 模拟新闻数据
const newsData = ref({
  id: 1,
  title: '橘事顺利影视工作室正式成立，鞠婧祎担任法定代表人',
  publishTime: '2024-12-10T14:30:00',
  author: '影视观察员',
  views: 12580,
  coverImage: 'https://via.placeholder.com/800x450',
  content: `
    <p>2024年12月9日，上海橘事顺利影视文化工作室（个人独资）在上海市松江区正式成立，法定代表人为知名艺人鞠婧祎。</p>
    <p>据公开信息显示，工作室注册地位于上海市松江区北松公路4915号16幢，经营范围包括：组织文化艺术交流活动；文艺创作；专业设计服务；电影摄制服务；技术服务、技术开发、技术咨询、技术交流、技术转让、技术推广；广告设计、代理；广告发布；广告制作；知识产权服务（专利代理服务除外）；社会经济咨询服务；影视美术道具置景服务；会议及展览服务；市场营销策划；文化娱乐经纪人服务；图文设计制作；租借道具活动；礼仪服务；咨询策划服务。</p>
    <p>鞠婧祎作为工作室法定代表人，表示将专注于优质影视内容的创作与开发，计划在未来三年内推出多部自制剧集和电影作品。</p>
    <p>行业分析人士指出，艺人成立个人工作室已成为影视行业新趋势，有利于艺人更好地掌控创作方向和商业发展。</p>
  `,
  images: [
    { url: 'https://via.placeholder.com/600x400', caption: '工作室注册地外景' },
    { url: 'https://via.placeholder.com/600x400', caption: '鞠婧祎近期活动照' }
  ],
  tags: ['影视行业', '工作室', '鞠婧祎', '创业']
})

// 相关新闻数据
const relatedNews = ref([
  {
    id: 2,
    title: '2024年影视行业发展趋势报告发布',
    cover: 'https://via.placeholder.com/300x200',
    time: '2024-11-28T09:15:00'
  },
  {
    id: 3,
    title: '鞠婧祎新剧《花间令》杀青，预计2025年播出',
    cover: 'https://via.placeholder.com/300x200',
    time: '2024-12-05T16:45:00'
  },
  {
    id: 4,
    title: '小型影视工作室如何在大厂竞争中突围',
    cover: 'https://via.placeholder.com/300x200',
    time: '2024-11-15T13:20:00'
  }
])

// 格式化日期
const formatDate = (dateString) => {
  const date = new Date(dateString)
  return `${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日 ${date.getHours()}:${date.getMinutes().toString().padStart(2, '0')}`
}

// 跳转到新闻详情
const goToNews = (id) => {
  router.push(`/news/${id}`)
}

// 模拟加载数据
onMounted(() => {
  // 这里可以替换为实际的API请求
  console.log('加载新闻数据:', newsData.value.id)
})
</script>

<style scoped>
.news-detail-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.breadcrumb {
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--el-border-color-light);
}

.news-main {
  background-color: #fff;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.news-header {
  margin-bottom: 30px;
  text-align: center;
}

.news-title {
  font-size: 28px;
  margin-bottom: 15px;
  color: #333;
  line-height: 1.4;
}

.news-meta {
  display: flex;
  justify-content: center;
  gap: 20px;
  color: #666;
  font-size: 14px;
  margin-bottom: 20px;
}

.news-meta .el-icon {
  margin-right: 5px;
}

.news-cover {
  margin-bottom: 30px;
}

.news-cover .el-image {
  width: 100%;
  max-height: 500px;
  border-radius: 4px;
}

.news-content {
  line-height: 1.8;
  font-size: 16px;
  color: #333;
  margin-bottom: 40px;
}

.news-content :deep(p) {
  margin-bottom: 20px;
  text-indent: 2em;
}

.gallery-title {
  font-size: 20px;
  margin: 40px 0 20px;
  color: #333;
  border-left: 4px solid var(--el-color-primary);
  padding-left: 10px;
}

.gallery-image {
  width: 100%;
  height: 200px;
  border-radius: 4px;
  margin-bottom: 10px;
}

.image-caption {
  text-align: center;
  color: #666;
  font-size: 14px;
  margin-bottom: 20px;
}

.image-error {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #999;
}

.news-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 40px;
  padding-top: 20px;
  border-top: 1px solid var(--el-border-color-light);
}

.news-tags {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.news-share {
  display: flex;
  align-items: center;
  gap: 10px;
}

.related-news {
  margin-top: 50px;
}

.related-title {
  font-size: 22px;
  margin-bottom: 30px;
  padding-bottom: 15px;
  border-bottom: 1px solid var(--el-border-color-light);
}

.related-card {
  margin-bottom: 20px;
  cursor: pointer;
  transition: transform 0.3s;
}

.related-card:hover {
  transform: translateY(-5px);
}

.related-cover {
  width: 100%;
  height: 180px;
}

.related-info {
  padding: 15px;
}

.related-info h4 {
  font-size: 16px;
  margin-bottom: 10px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.related-time {
  color: #666;
  font-size: 14px;
}

@media (max-width: 768px) {
  .news-main {
    padding: 20px;
  }

  .news-title {
    font-size: 22px;
  }

  .news-meta {
    flex-direction: column;
    gap: 8px;
    align-items: center;
  }

  .news-footer {
    flex-direction: column;
    gap: 20px;
  }

  .news-share {
    width: 100%;
    justify-content: center;
  }
}
</style>